<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <div :style="{ backgroundColor: '#F5F5F5' }">
        <span>预订信息</span>
        <el-row>
          <el-col :span="8">
            <el-form-item label="客源信息" prop="touristInfo">
              <el-select v-model="ruleForm.touristInfo" placeholder="请选择">
                <el-option label="请选择" value="0"></el-option>
                <el-option label="非会员" value="非会员"></el-option>
                <el-option label="会员" value="会员"></el-option>
                <el-option label="协议公司" value="协议公司"></el-option>
                <el-option label="订房中心" value="订房中心"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="订单来源">
              <el-select v-model="ruleForm.rrderSource" placeholder="请选择">
                <el-option label="非会员" value="1"></el-option>
                <el-option label="会员" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="预订姓名" prop="reservation">
              <el-input
                v-model="ruleForm.reservation"
                placeholder="请输入预定姓名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="手机号码" prop="phone">
              <el-input
                v-model="ruleForm.phone"
                placeholder="请输入手机号码"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="预订单号(外)" prop="reservationId">
              <el-input
                v-model="ruleForm.reservationId"
                placeholder="请输入预订单号"
                @input="tianjiadanhao"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </div>
      <div :style="{ backgroundColor: '#F5F5F5' }">
        <span>房间信息</span>
        <el-row>
          <el-col :span="8">
            <el-form-item label="入住日期" prop="checkDate">
              <el-date-picker
                v-model="ruleForm.checkDate"
                type="date"
                format="yyyy-MM-dd"
                placeholder="请输入入住日期"
                :picker-options="pickerOptionsCheckIn"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="离店日期" prop="checkOut">
              <el-date-picker
                v-model="ruleForm.checkOut"
                type="date"
                placeholder="请输入离店日期"
                :picker-options="pickerOptionsCheckOut"
                @change="jisuanriqi"
                format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入住天数" prop="days">
              <el-input
                :disabled="true"
                v-model="ruleForm.days"
                placeholder="请输入预订单号"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-table
              ref="multipleTable"
              :data="houseroomstatelist"
              tooltip-effect="dark"
              style="width: 90%; text-align: center"
              @selection-change="handleSelectionChange"
            >
              <el-table-column type="selection" width="55"> </el-table-column>
              <el-table-column prop="houseName" label="房型列表">
              </el-table-column>
              <el-table-column prop="roomNumInt" label="可定数">
              </el-table-column>
              <el-table-column prop="housePrice" label="门市价">
              </el-table-column>
              <el-table-column prop="averagePrice" label="房间均价">
              </el-table-column>
              <el-table-column label="房间数量">
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.roomNumber"
                    placeholder="0"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="roomNum" label="排房"> </el-table-column>
              <el-table-column label="自定义价格">
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.averagePrice"
                    @change="genghuanjiage(scope.row)"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column>
                <template slot-scope="scope">
                  <el-button type="text" @click="houseprice(scope.row)"
                    >设置房价</el-button
                  >
                  <el-button type="text" @click="houserank(scope.row)"
                    >排房</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <el-dialog title="设置房价" :visible.sync="dialogTableVisible">
              <el-table :data="datalist" style="width: 100%">
                <el-table-column prop="date" label="周一">
                  <template slot-scope="aa">
                    <el-input
                      v-model="custommoney"
                      v-show="aa.row.week1 == nowdate"
                      style="width: 50%"
                    ></el-input>
                    <br /><br />
                    {{ aa.row.week1 }}
                  </template>
                </el-table-column>
                <el-table-column prop="date" label="周二">
                  <template slot-scope="aa">
                    <el-input
                      v-model="custommoney"
                      v-show="aa.row.week2 == nowdate"
                      style="width: 50%"
                    ></el-input>
                    <br /><br />
                    {{ aa.row.week2 }}
                  </template>
                </el-table-column>
                <el-table-column prop="date" label="周三">
                  <template slot-scope="aa">
                    <el-input
                      v-model="custommoney"
                      v-show="aa.row.week3 == nowdate"
                      style="width: 50%"
                    ></el-input>
                    <br /><br />
                    {{ aa.row.week3 }}
                  </template>
                </el-table-column>
                <el-table-column prop="date" label="周四">
                  <template slot-scope="aa">
                    <el-input
                      v-model="custommoney"
                      v-show="aa.row.week4 == nowdate"
                      style="width: 50%"
                    ></el-input>
                    <br /><br />
                    {{ aa.row.week4 }}
                  </template>
                </el-table-column>
                <el-table-column prop="date" label="周五">
                  <template slot-scope="aa">
                    <el-input
                      v-model="custommoney"
                      v-show="aa.row.week5 == nowdate"
                      style="width: 50%"
                    ></el-input>
                    <br /><br />
                    {{ aa.row.week5 }}
                  </template>
                </el-table-column>
                <el-table-column prop="date" label="周六">
                  <template slot-scope="aa">
                    <el-input
                      v-model="custommoney"
                      v-show="aa.row.week6 == nowdate"
                      style="width: 50%"
                    ></el-input>
                    <br /><br />
                    {{ aa.row.week6 }}
                  </template>
                </el-table-column>
                <el-table-column prop="date" label="周日">
                  <template slot-scope="aa">
                    <el-input
                      v-model="custommoney"
                      v-show="aa.row.week7 == nowdate"
                      style="width: 50%"
                    ></el-input>
                    <br /><br />
                    {{ aa.row.week7 }}
                  </template>
                </el-table-column>
              </el-table>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogTableVisible = false">取 消</el-button>
                <el-button type="primary" @click="fangjia">确 定</el-button>
              </div>
            </el-dialog>
          </el-col>
        </el-row>
      </div>
      <div :style="{ backgroundColor: '#F5F5F5' }">
        <span>商户备注</span>
        <el-row>
          <el-col :span="24">
            <el-form-item label="商户备注">
              <el-input
                type="textarea"
                v-model="ruleForm.MerchantRemarks"
                maxlength="300"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </div>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确定</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  gethouse,
  getByidhousetypelist,
  Updzidingyiprice,
} from "@/api/housetype";
import { houseSnowId, AddHouseState, housedatemeony } from "@/api/housestate";
export default {
  name: "VueElementAdminFixInstallProblemReserveHouse",

  data() {
    return {
      ruleForm: {
        touristInfo: "",
        reservation: "",
        rrderSource: "",
        phone: "",
        reservationId: "",
        checkDate: "",
        checkOut: "",
        days: 0,
        houseType: "",
        rackRate: 0,
        averagePrice: 0,
        roomNum: 0,
        rowHouse: "",
        merchantRemarks: "",
      },
      dialogTableVisible: false,
      custommoney: 0,
      multipleSelection: [],
      ReserveList: [],
      rules: {
        touristInfo: [
          { required: true, message: "请输入客源信息", trigger: "blur" },
        ],
        reservation: [
          { required: true, message: "请输入预订姓名", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^1[34578]\d{9}$/,
            message: "手机号码格式不正确",
            trigger: "blur",
          },
        ],
        checkDate: [
          { required: true, message: "入住日期不能为空", trigger: "blur" },
        ],
        checkOut: [
          { required: true, message: "离店日期不能为空", trigger: "blur" },
        ],
        days: [
          { required: true, message: "入住天数不能为空", trigger: "blur" },
        ],
      },
      //禁止选择今天之前的日期
      pickerOptionsCheckIn: {
        disabledDate: (time) => {
          const today = new Date();
          today.setHours(0, 0, 0, 0); // 设置为当天的零点
          return time.getTime() < today.getTime();
        },
      },
      pickerOptionsCheckOut: {
        disabledDate: (time) => {
          const today = new Date();
          today.setHours(0, 0, 0, 0); // 设置为当天的零点
          return (
            time.getTime() < today.getTime() ||
            time.getTime() < this.ruleForm.checkDate
          );
        },
      },
      housetypelist: [],
      houseroomstatelist: [],
      //设置房价
      datalist: [],
      nowdate: "",
      housetypeid: 0,
      name: "",
    };
  },

  mounted() {
    this.getroomstate();
    this.ruleForm.roomMoney = this.custommoney;
    //设置房价
    let datenow = new Date();
    let y = datenow.getFullYear();
    let m = datenow.getMonth() + 1;
    let r = datenow.getDate();

    if (m < 10) {
      m = "0" + m;
    }

    if (r < 10) {
      r = "0" + r;
    }

    let d = y + "-" + m + "-" + r;
    this.nowdate = d;
    this.getdate();
  },

  methods: {
    //更换自定义价格
    genghuanjiage(rows) {
      Updzidingyiprice(rows.id, rows.averagePrice).then((res) => {
        if (res.data) {
          this.$message({
            message: res.Message,
            type: "success",
          });
        } else {
          this.$message({
            message: res.Message,
            type: "warning",
          });
        }
      });
    },
    //设置房价
    fangjia() {
      console.log(this.housetypeid, this.custommoney);
      Updzidingyiprice(this.housetypeid, parseInt(this.custommoney)).then(
        (res) => {
          if (res.data) {
            this.$message({
              message: res.message,
              type: "success",
            });
            this.getroomstate();
          } else {
            this.$message({
              message: res.message,
              type: "warning",
            });
          }
        }
      );
      this.dialogTableVisible = false;
    },
    getdate() {
      housedatemeony().then((res) => {
        this.datalist = res.data;
      });
    },
    //计算天数
    jisuanriqi() {
      this.ruleForm.days =
        (this.ruleForm.checkOut - this.ruleForm.checkDate) /
        (1000 * 60 * 60 * 24);
    },
    //添加预订单号
    tianjiadanhao() {
      houseSnowId().then((res) => {
        this.ruleForm.reservationId = res.data;
      });
    },
    //房型筛选
    getroomstate() {
      gethouse().then((res) => {
        this.houseroomstatelist = res.data;
      });
    },
    //设置房价
    houseprice(row) {
      this.housetypeid = row.id;
      this.custommoney = row.averagePrice;
      getByidhousetypelist(row.id).then((res) => {
        this.custommoney = res.data.averagePrice;
      });

      this.dialogTableVisible = true;
    },
    //排房
    houserank(rows) {
      console.log(rows);
      if (rows.roomNumber == 0) {
        this.$message({
          message: "房间数量需要大于0",
          type: "warning",
        });
      }
      if (rows.roomNumber > rows.roomNumInt) {
        this.$message({
          message: "房间数量不能大于可定房间数",
          type: "warning",
        });
      }
    },
    //办理预定
    submitForm(formName) {
      this.ReserveList = [];
      this.multipleSelection.forEach((ele) => {
        const reserveItem = { ...this.ruleForm };
        reserveItem.touristInfo = this.ruleForm.touristInfo;
        reserveItem.reservation = this.ruleForm.reservation;
        reserveItem.rrderSource = this.ruleForm.rrderSource;
        reserveItem.phone = this.ruleForm.phone;
        reserveItem.reservationId = this.ruleForm.reservationId.toString();
        reserveItem.checkDate = this.ruleForm.checkDate;
        reserveItem.checkOut = this.ruleForm.checkOut;
        reserveItem.days = this.ruleForm.days;
        reserveItem.houseType = ele.houseName;
        console.log(reserveItem.houseType);
        reserveItem.rackRate = ele.housePrice;
        reserveItem.averagePrice = ele.averagePrice;
        reserveItem.roomNum = ele.roomNumber;
        reserveItem.rowHouse = ele.roomNum;
        reserveItem.merchantRemarks = this.ruleForm.merchantRemarks;
        this.ReserveList.push(reserveItem);
      });
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ReserveList.forEach((item) => {
            let checkd = [];
            checkd.push(item);
            console.log(checkd);
            AddHouseState(checkd).then((res) => {
              if (res.data == null) {
                this.$message({
                  message: res.message,
                  type: "success",
                });
              } else {
                this.$message({
                  message: res.message,
                  type: "warning",
                });
              }
            });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
  },
};
</script>